/*
 * @Description: CRM
 * @Author: lwp
 * @Date: 2021-05-18 11:53:36
 * @LastEditTime: 2021-12-24 14:30:10
 * @LastEditors: lwp
 */
import { Message, MessageBox } from 'element-ui'
// import Vue from 'vue'
const success = (msg, fun = Function, duration = 2000) => {
  return new Promise((resolve, reject) => {
    duration = duration || 2000
    Message({
      message: msg,
      type: 'success',
      duration: duration
    })
    setTimeout(() => {
      resolve(fun())
    }, duration)
  })
}

const error = (msg, offset) => {
  Message({
    message: msg,
    type: 'error',
    duration: 1500,
    offset: offset || 20
  })
}
const delayeOff = (title, msg, confirmFn, time = 3, type = 'success', style) => {
  const self = this
  let timeOutIns
  const h = window.app.$createElement
  style = style || 'color:#FF0000; font-weight: 700;font-size: 16px;'
  MessageBox({
    title: title,
    type: type,
    center: true,
    confirmButtonText: `确定`,
    message: h('p', null, [
      h('span', { style }, msg)
    ]),
    closeOnPressEscape: false,
    closeOnClickModal: false,
    confirmButtonClass: 'wp-messageBox__delayeOff-but',
    cancelButtonClass: 'wp-messageBox__delayeOff',
    showCancelButton: true,
    callback: (action, instance) => {
      if (action === 'confirm' || action === 'cancel') {
        confirmFn()
      }
    },
    beforeClose: (action, instance, done) => {
      if (action === 'cancel') {
        if (timeOutIns) {
          clearTimeout(timeOutIns)
        }
        self.timeOut(instance, done, timeOutIns)
      } else {
        if (timeOutIns) {
          clearTimeout(timeOutIns)
        }
        done()
      }
    }
  })

  self.timeOut = function (instance, done) {
    if (time > 0) {
      instance.confirmButtonText = `（ ` + time + ` ）s 确 定`
      time--
      if (timeOutIns) {
        clearTimeout(timeOutIns)
      }
      timeOutIns = setTimeout(() => {
        self.timeOut(instance, done)
      }, 1000)
    } else {
      done()
    }
  }
  setTimeout(() => {
    document.getElementsByClassName('wp-messageBox__delayeOff')[0].click()
  }, 1000)
}
const confirm = (msg, confirmFn, cancelFn, title = '提示') => {
  MessageBox({
    title: title,
    message: msg,
    showCancelButton: true,
    cancelButtonText: '取消',
    confirmButtonText: '确定',
    type: 'warning',
    beforeClose: function (action, instance, done) {
      // action :confirm cancel close
      if (confirmFn !== undefined && confirmFn !== null && action === 'confirm') {
        // instance.confirmButtonLoading = true
        // instance.confirmButtonText = '执行中...'
        // setTimeout(() => {
        //   confirmFn()
        //   done()
        //   setTimeout(() => {
        //     instance.confirmButtonLoading = false
        //   }, 300)
        // }, 3000)
        confirmFn()
        done()
      } else if (cancelFn !== undefined && cancelFn !== null && action === 'cancel') {
        cancelFn()
      } else {
        done()
      }
    }
  })
}
export default{
  success,
  error,
  confirm,
  delayeOff
}
